package mysql

import (
	"fresh-go/freshrpc/freshrpc"
	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	FreshName  string  `gorm:"type:varchar(30)"`
	FreshPrice float32 `gorm:"type:decimal(10,2)"`
	FreshNum   int     `gorm:"type:int"`
	FreshTypes string  `gorm:"type:varchar(30)"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Fresh) FreshShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(f).Error
}

func (f *Fresh) FreshShowByName(db *gorm.DB, name string) error {
	return db.Debug().Where("fresh_name=?", name).Find(f).Error
}

func FreshList(db *gorm.DB, in *freshrpc.FreshListRequest) ([]*freshrpc.FreshList, error) {

	var list []*freshrpc.FreshList

	err2 := db.Debug().Model(&Fresh{}).Select(
		"id as fresh_id",
		"fresh_name",
		"fresh_price",
		"fresh_num",
		"fresh_types",
	).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error

	return list, err2
}
